package com.br.mercarad;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.BoxLayout;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import com.br.mercarad.db.DbManagerSqLite;
import com.br.mercarad.db.vos.CampoVO;
import com.br.mercarad.db.vos.TabelaVO;

public class DialogDescTable extends JDialog {
	private JTextField txtSulfixo;
	private DbManagerSqLite manager;
	private JTable table;
	private DefaultTableModel model;
	private JTextField txtNomeTabela;
	private TabelaVO tabela;
	private JEditorPane editorPanelDdl;
	private JEditorPane editorPanelTables;
	private JEditorPane editorPanelVO;

	public DialogDescTable(DbManagerSqLite manager, String tableName) {
		this.manager = manager;

		setBounds(100, 100, 800, 600);
		getContentPane().setLayout(new BorderLayout());
		{
			JPanel panel = new JPanel();
			getContentPane().add(panel, BorderLayout.NORTH);
			panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
			{
				JLabel label = new JLabel("Tabela");
				panel.add(label);
			}
			{
				txtNomeTabela = new JTextField();
				txtNomeTabela.setPreferredSize(new Dimension(50, 20));
				txtNomeTabela.setMinimumSize(new Dimension(28, 20));
				txtNomeTabela.setText("<dynamic>");
				txtNomeTabela.setColumns(10);
				panel.add(txtNomeTabela);
			}
			{
				txtSulfixo = new JTextField();
				panel.add(txtSulfixo);
				txtSulfixo.setColumns(10);
			}
		}
		{
			JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
			getContentPane().add(tabbedPane, BorderLayout.CENTER);
			{
				JScrollPane scrollPane = new JScrollPane();
				tabbedPane.addTab("Estrutura", null, scrollPane, null);
				{
					table = new JTable();
					scrollPane.setViewportView(table);
				}
			}
			{
				JScrollPane scrollPane = new JScrollPane();
				tabbedPane.addTab("Tables", null, scrollPane, null);
				{
					editorPanelTables = new JEditorPane();
					editorPanelTables.setText((String) null);
					scrollPane.setViewportView(editorPanelTables);
				}
			}
			{
				JScrollPane scrollPane = new JScrollPane();
				tabbedPane.addTab("VO", null, scrollPane, null);
				{
					editorPanelVO = new JEditorPane();
					editorPanelVO.setText((String) null);
					scrollPane.setViewportView(editorPanelVO);
				}
			}
			{
				JScrollPane scrollPane = new JScrollPane();
				tabbedPane.addTab("DataAcess", null, scrollPane, null);
			}
			{
				JScrollPane scrollPane = new JScrollPane();
				tabbedPane.addTab("DDL", null, scrollPane, null);
				{
					editorPanelDdl = new JEditorPane();
					scrollPane.setViewportView(editorPanelDdl);
				}
			}
		}
		getTableDesc(tableName);
	}

	private void getTableDesc(String tableName) {
		tabela = this.manager.getCamposFromTable(tableName);
		model = new DefaultTableModel();
		table.setModel(model);
		// Cria duas colunas
		model.addColumn("Nome");
		model.addColumn("Tipo");
		model.addColumn("NN");
		model.addColumn("PK");

		for (CampoVO campo : tabela.getCampos()) {
			model.addRow(new Object[] { campo.getNome(), campo.getTypeDesc(), campo.getIsNull(), campo.getIsPrimary() });
		}

		txtNomeTabela.setText(tabela.getNome());
		txtSulfixo.setText(tabela.getSulfixo());
		editorPanelDdl.setText(tabela.getDdlFormated());
		editorPanelTables.setText(getTablesJava(tabela));
		editorPanelVO.setText(getVOJava(tabela));
	}

	private String getTablesJava(TabelaVO tabela) {
		String aspa = "\"", pv = ";";
		String aux;
		StringBuilder ret = new StringBuilder();
		ret.append("public static class " + tabela.getNome() + "{");
		ret.append("public final static String table = " + aspa + tabela.getNome() + aspa + pv);
		for (CampoVO campo : tabela.getCampos()) {
			aux = campo.getNome().replaceAll("_" + tabela.getSulfixo(), "");
			ret.append("public final static String " + aux + " = " + aspa + campo.getNome() + aspa + pv);
		}
		ret.append("}");
		return ret.toString();
	}

	private String getVOJava(TabelaVO tabela) {
		String aspa = "\"", pv = ";";
		String aux;
		StringBuilder ret = new StringBuilder();

		ret.append("public class " + tabela.getNomeSing() +" extends Entity { ");
		for (CampoVO campo : tabela.getCampos()) {
			aux = campo.getNome().replaceAll("_" + tabela.getSulfixo(), "");
			ret.append("private " + campo.getTypeJavaPrimitive() + " " + aux + pv);
		}

		ret.append("private I" + tabela.getNomeInitCapSingular() + "DataAccess dataAccess;");
		ret.append("\n");
		ret.append("public "+tabela.getNomeSing()+"() {");

		ret.append("dataAccess = DataAccessFactory.getCurrent().get" + tabela.getNomeInitCapSingular() + "DataAccess();");
		ret.append("}");
		// dataAccess = DataAccessFactory.getCurrent().getPedidoDataAccess();
		ret.append("\n");
		ret.append("public "+tabela.getNomeSing()+"(" + tabela.getPksWithTypes() + ") {");
		ret.append("this();");
		ret.append("load(dataAccess.findByID(pkimplementar)); }");
		ret.append("\n");
		ret.append("@Override ");
		ret.append("protected void load(DataMap map) { ");
		ret.append("if (map == null) ");
		ret.append("return; ");

		for (CampoVO campo : tabela.getCampos()) {
			ret.append(campo.getNome().replaceAll("_" + tabela.getSulfixo(), "") + " = map.<" + campo.getTypeJavaObject() + ">get(\""
					+ campo.getNome().replaceAll("_" + tabela.getSulfixo(), "") + "\");");

		}
		ret.append("}");
		ret.append("\n");

		ret.append("@Override ");
		ret.append("public void save() {");
		ret.append("dataAccess.save(this);");
		ret.append("}");
		ret.append("\n");
		ret.append("@Override ");
		ret.append("public void update() {");
		ret.append("dataAccess.update(this);");
		ret.append("}");
		ret.append("\n");
		ret.append("@Override ");
		ret.append("public void delete() {");
		ret.append("dataAccess.delete(this);");
		ret.append("}");
		ret.append("}");

		return ret.toString();

		// load(dataAccess.findByID(codigo));
		// }
		//
		// @Override
		// protected void load(DataMap map) {
		// if (map == null)
		// return;
		//
		// }
		//
		// @Override
		// public void save() {
		// dataAccess.save(this);
		// }
		//
		// @Override
		// public void update() {
		// dataAccess.update(this);
		// }
		//
		// @Override
		// public void delete() {
		// dataAccess.delete(this);
		// }
		//

	}

}
